package xyz.robinbingo.rbtask.generator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import xyz.robinbingo.rbtask.common.constant.Constants;

/**
 * @author Robin
 * @date 2018-08-24 14:17
 **/
class Generator {

    private static final String PACKAGE_NAME = "xyz.robinbingo.rbtask";
    private static final String OUTPUT_DIR = Constants.PROJ_PATH + "src\\main\\java";
    private static final String DRIVER_NAME = "com.mysql.cj.jdbc.Driver";

    private static final String SUPER_ENTITY = PACKAGE_NAME + ".bean.po.base.BasePo";
    private static final String[] SUPER_ENTITY_COLUMNS = {"id", "create_time", "created_by", "modify_time", "modified_by", "is_deleted"};
    private static final String SUPER_MAPPER = PACKAGE_NAME + ".mapper.base.CustomBaseMapper";
    private static final String SUPER_SERVICE_IMPL = PACKAGE_NAME + ".service.impl.base.CustomBaseServiceImpl";

    static void g(String url, String username, String password, String[] tableNames) {

        GlobalConfig globalConfig = new GlobalConfig()
                .setActiveRecord(false)
                .setAuthor("Robin")
                .setOutputDir(OUTPUT_DIR)
                .setFileOverride(true)
                .setDateType(DateType.ONLY_DATE)
                .setOpen(false)
                .setServiceName("%sService");

        DataSourceConfig dataSourceConfig = new DataSourceConfig()
                .setDbType(DbType.MYSQL)
                .setUrl(url)
                .setUsername(username)
                .setPassword(password)
                .setDriverName(DRIVER_NAME);

        StrategyConfig strategyConfig = new StrategyConfig()
                .setSuperEntityClass(SUPER_ENTITY)
                .setSuperEntityColumns(SUPER_ENTITY_COLUMNS)
                .setSuperMapperClass(SUPER_MAPPER)
                .setSuperServiceImplClass(SUPER_SERVICE_IMPL)
                .setCapitalMode(true)
                .setEntityLombokModel(true)
                .setNaming(NamingStrategy.underline_to_camel)
                .setInclude(tableNames);

        PackageConfig packageConfig = new PackageConfig()
                .setParent(PACKAGE_NAME)
                .setEntity("bean.po");

        TemplateConfig templateConfig = new TemplateConfig()
                .setController("");

        new AutoGenerator()
                .setGlobalConfig(globalConfig)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(packageConfig)
                .setTemplate(templateConfig)
                .execute();
    }
}
